package com.uairobot.bill.yz.office.agent;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.util.StringUtils;

import com.litong.utils.ip.IPUtil;

@Configuration
@Import(ImportAutoConfiguration.class)
@ComponentScan(value = "com.uairobot.bill.yz.office.agent", excludeFilters = {
    @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
    @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

@EnableJpaRepositories(basePackages = { "com.uairobot.bill.yz.office.agent.repository" })
@EntityScan("com.uairobot.bill.yz.office.agent.entity")
public class Application {
  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    SpringApplication app = new SpringApplication(Application.class);
    app.setBannerMode(Banner.Mode.OFF);
    ApplicationContext context = app.run(args);
    info(start, context);
  }

  private static void info(long start, ApplicationContext ctx) {
    ServerProperties sp = ctx.getBean(ServerProperties.class);
    int port = sp.getPort();
    String contextPath = sp.getContextPath();
    Environment environment = ctx.getBean(Environment.class);
    String projectName = environment.getProperty("spring.application.name");
    if (StringUtils.isEmpty(projectName))
      projectName = "bill-yz-office-agent";
    String thisUrl = IPUtil.getThisUrl(port, contextPath);
    long end = System.currentTimeMillis();
    System.out.println(projectName + "started " + (end - start) + "ms");
    System.out.println(thisUrl);
  }
}